package net.entelijan.cobean.examples.lit;

import java.awt.event.ActionEvent;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.List;

import javax.swing.JOptionPane;

import net.entelijan.cobean.core.ColumnDesc;
import net.entelijan.cobean.data.literature.ILitService;
import net.entelijan.cobean.data.literature.LitAuthor;
import net.entelijan.config.CobeanConfigFactory;
import net.entelijan.config.ICobeanConfig;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class MainFrameController implements IEditAuthorDialogParent {

	private static Log log = LogFactory.getLog(MainFrameController.class);

	private MainFrameModel model;

	private ILitService litService;

	private MainFrame view;

	public MainFrameController(MainFrameModel pmodel, MainFrame view,
			ILitService litService) {
		super();
		this.view = view;
		this.litService = litService;
		this.model = pmodel;

		addActionListeners();

		// Bind the view to the model using convetion over configuration.
		ICobeanConfig cfg = CobeanConfigFactory.createConfig();
		model = cfg.bind(model, view, null);
		model = cfg.bind(model, view.getAuthorListPanel(), ColumnDesc
				.createFilterableSortableColumnDescs(LitAuthor.class,
						"name,Name", "birthday,Birthday"));
	}

	private void addActionListeners() {
		view.getAuthorListPanel().getRefreshButton().addActionListener(
				new DefaultActionListener("Refresh Authors") {

					public void actionPerformedIntern(ActionEvent e) {
						List<LitAuthor> authors = litService.loadAllAuthors();
						model.setAuthors(authors);
					}
				});
		view.getAuthorListPanel().getNewButton().addActionListener(
				new DefaultActionListener("New Author") {

					public void actionPerformedIntern(ActionEvent e) {
						EditAuthorDialog dialog = new EditAuthorDialog(view);
						dialog.setModal(true);
						dialog.setSize(500, 250);
						dialog.setLocation(view.getX() + 10, view.getY() + 10);
						EditAuthorDialogController cnt = new EditAuthorDialogController(
								new LitAuthor(), dialog, litService,
								MainFrameController.this);
						cnt.open();
						log.info("[NewAuthorButton] returned from open");
					}
				});
		view.getAuthorListPanel().getEditButton().addActionListener(
				new DefaultActionListener("Edit Author") {

					public void actionPerformedIntern(ActionEvent e) {
						if (model.getAuthorsSelectedValue() == null) {
							showWarning("You must select an author!");
						} else {
							EditAuthorDialog dialog = new EditAuthorDialog(view);
							dialog.setModal(true);
							dialog.setSize(500, 250);
							dialog.setLocation(view.getX() + 10,
									view.getY() + 10);
							EditAuthorDialogController cnt = new EditAuthorDialogController(
									model.getAuthorsSelectedValue(), dialog,
									litService, MainFrameController.this);
							cnt.open();
						}
					}

				});
		view.getAuthorListPanel().getDeleteButton().addActionListener(
				new DefaultActionListener("Delete Author") {

					public void actionPerformedIntern(ActionEvent e) {
						LitAuthor sel = model.getAuthorsSelectedValue();
						if (sel == null) {
							showWarning("You must select an author !");
						} else {
							if (confirme("Do you really want to delete '"
									+ sel.getName() + "'")) {
								litService.deleteBook(sel.getId());
								refreshAuthorList();
							}
						}
					}

				});
		view.getBookListPanel().getSearchButton().addActionListener(
				new DefaultActionListener("Search Books") {

					public void actionPerformedIntern(final ActionEvent e) {
						litService.findBooks(model.getSearchItem());
					}
				});

		view.getAuthorListPanel().addComponentListener(new ComponentAdapter() {
			@Override
			public void componentShown(ComponentEvent e) {
				List<LitAuthor> authors = litService.loadAllAuthors();
				model.setAuthors(authors);
			}
		});
	}

	public void open() {
		this.view.setVisible(true);

	}

	@Override
	public void refreshAuthorList() {
		log.info("[refreshAuthorList]");
		List<LitAuthor> authors = litService.loadAllAuthors();
		model.setAuthors(authors);
	}

	private void showWarning(String msg) {
		JOptionPane.showMessageDialog(view, msg, view.getTitle(),
				JOptionPane.WARNING_MESSAGE);
	}

	private boolean confirme(String msg) {
		return JOptionPane.showConfirmDialog(view, msg, view.getTitle(),
				JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION;
	}
}
